<?php

class GatewayController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array( 
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('admin','view'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('index','view','admin','delete','create','update','gatewayMap'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Gateway;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Gateway']))
		{
			$model->attributes=$_POST['Gateway'];  

			if(in_array($model->citys, array('北京市','上海市','天津市','重庆市'))){
				$model->provinces = "直辖市";
			}
			if(in_array($model->citys, array('香港特别行政区','澳门特别行政区'))){
				$model->provinces = "行政区";
			}

			if($model->save())
				$this->redirect(array('admin','id'=>$model->id));
		} 
		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Gateway']))
		{
			$model->attributes=$_POST['Gateway'];

			if(in_array($model->citys, array('北京市','上海市','天津市','重庆市')) && $model->provinces!="直辖市"){
				$model->provinces = "直辖市";
			}
			if(in_array($model->citys, array('香港特别行政区','澳门特别行政区')) && $model->provinces!="行政区"){
				$model->provinces = "行政区";
			}

			if($model->save())
				$this->redirect(array('admin','id'=>$model->id));
		}

		$model->len_lat=$model->lng.",".$model->lat;

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		// $dataProvider=new CActiveDataProvider('Gateway');
		// $this->render('index',array(
		// 	'dataProvider'=>$dataProvider,
		// ));
		$this->redirect(array('admin'));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		 
		$model=new Gateway('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Gateway']))
			$model->attributes=$_GET['Gateway'];

		if(Yii::app()->user->name !=="admin"){
		   $model->companyid=Yii::app()->user->id;
	    } 
		$this->render('admin',array(
			'model'=>$model,
		));
	}


	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Gateway the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Gateway::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Gateway $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='gateway-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

	/**
	 * undocumented function
	 * 
	 **/
	public function actionGatewayMap()
	{
		if(!empty($_GET['city'])){
			//得到要查看的城市
			$city=$_GET['city'];
			//查询出这个城市所有的路由地址
			$model=array();
			$models = Gateway::model()->findAll('citys=:city',array(':city'=>$city));
			foreach ($models as $key => $value) {
				$model[$key]['lng']=$value->lng;
				$model[$key]['lat']=$value->lat; 
				$model[$key]['address']="企业:".$value->company->title.'<br />'."路由:".$value->gatewayname.'<br />'.$value->address; 
			}
			//返回页面并显示
			return $this->render('gatewayMapView',array(
				'model'=>$model,'city'=>$city,
		   )); 
		}
		//循环判断哪些城市已经布点 
		$gatewaycity = Gateway::model()->findAllBySql("select provinces,citys from ".Gateway::model()->tableName()." group by provinces,citys "); 
		$citys = $this->city();
		$viewcitys=array();
		foreach ($citys as $key => $value) {
		 	foreach ($gatewaycity as $k => $v) {
			 	if($value['mName']==$v->provinces && !array_key_exists($value['mName'], $viewcitys)) { 
			 	    $viewcitys[$value['mName']]['mName'] = $value['mName'];
			 	    $viewcitys[$value['mName']]['mItems'] = array(); 
			 	}  
			 }
		} 
		foreach ($gatewaycity as $key => $value) { 
			if (!in_array($value->citys, $viewcitys[$value->provinces])){
				$viewcitys[$value->provinces]['mItems'][]=$value->citys;
			}  
		} 
		$this->render('gatewayMap',array(
			'model'=>$viewcitys,
		));

	}

	/**
	 * 全国所有的省市
	 * 
	 **/
	private static function city(){
		return array(
			array('mName'=>'直辖市','mItems' => array ('北京市','上海市','天津市','重庆市')), 
			array('mName'=>'行政区','mItems' => array ('香港特别行政区','澳门特别行政区')), 
			array('mName'=>'广东省',
			'mItems' => array ('广州市','韶关市','深圳市','珠海市','汕头市','佛山市','江门市','湛江市','茂名市','肇庆市','惠州市','梅州市','汕尾市','河源市','阳江市','清远市','东莞市','中山市','潮州市','揭阳市','云浮市')),
			array('mName'=>'浙江省',
			'mItems' => array ('杭州市','宁波市','温州市','嘉兴市','湖州市','绍兴市','金华市','衢州市','舟山市','台州市','丽水市')),     
			array('mName'=>'江苏省',
			'mItems' => array ('南京市','无锡市','徐州市','常州市','苏州市','南通市','连云港市','淮安市','盐城市','扬州市','镇江市','泰州市','宿迁市')),
			array('mName'=>'福建省',
			'mItems' => array ('福州市','厦门市','莆田市','三明市','泉州市','漳州市','南平市','龙岩市','宁德市')),
			array('mName'=>'湖南省',
			'mItems' => array ('长沙市','株洲市','湘潭市','衡阳市','邵阳市','岳阳市','常德市','张家界市','益阳市','郴州市','永州市','怀化市','娄底市','湘西土家族苗族自治州')),
			array('mName'=>'湖北省',
			'mItems' => array ('武汉市','黄石市','十堰市','宜昌市','襄樊市','鄂州市','荆门市','孝感市','荆州市','黄冈市','咸宁市','随州市','恩施土家族苗族自治州','仙桃市','潜江市','天门市','神农架林区')),
			array('mName'=>'辽宁省',
			'mItems' => array ('沈阳市','大连市','鞍山市','抚顺市','本溪市','丹东市','锦州市','营口市','阜新市','辽阳市','盘锦市','铁岭市','朝阳市','葫芦岛市')),
			array('mName'=>'吉林省',
			'mItems' => array ('长春市','吉林市','四平市','辽源市','通化市','白山市','松原市','白城市','延边朝鲜族自治州')),
			array('mName'=>'黑龙江省',
			'mItems' => array ('哈尔滨市','齐齐哈尔市','鸡西市','鹤岗市','双鸭山市','大庆市','伊春市','佳木斯市','七台河市','牡丹江市','黑河市','绥化市','大兴安岭地区')),
			array('mName'=>'河北省',
			'mItems' => array ('石家庄市','唐山市','秦皇岛市','邯郸市','邢台市','保定市','张家口市','承德市','沧州市','廊坊市','衡水市')),
			array('mName'=>'河南省',
			'mItems' => array ('郑州市','开封市','洛阳市','平顶山市','安阳市','鹤壁市','新乡市','焦作市','濮阳市','许昌市','漯河市','三门峡市','南阳市','商丘市','信阳市','周口市','驻马店市','济源市')),
			array('mName'=>'山东省',
			'mItems' => array ('济南市','青岛市','淄博市','枣庄市','东营市','烟台市','潍坊市','济宁市','泰安市','威海市','日照市','莱芜市','临沂市','德州市','聊城市','滨州市','菏泽市')),
			array('mName'=>'陕西省',
			'mItems' => array ('西安市','铜川市','宝鸡市','咸阳市','渭南市','延安市','汉中市','榆林市','安康市')),
			array('mName'=>'甘肃省',
			'mItems' => array ('兰州市','嘉峪关市','金昌市','白银市','天水市','武威市','张掖市','平凉市','酒泉市','庆阳市','定西市','陇南市','临夏回族自治州','甘南藏族自治州')),
			array('mName'=>'青海省',
			'mItems' => array ('西宁市','海东地区','海北藏族自治州','黄南藏族自治州','海南藏族自治州','果洛藏族自治州','玉树藏族自治州','海西蒙古族藏族自治州')),
			array('mName'=>'新疆维吾尔自治区',
			'mItems' => array ('乌鲁木齐市','克拉玛依市','吐鲁番地区','哈密地区','昌吉回族自治州','博尔塔拉蒙古自治州','巴音郭楞蒙古自治州','阿克苏地区','克孜勒苏柯尔克孜自治州','喀什地区','和田地区','伊犁哈萨克自治州','塔城地区','阿勒泰地区','石河子市','阿拉尔市','图木舒克市','五家渠市')),
			array('mName'=>'山西省',
			'mItems' => array ('太原市','大同市','阳泉市','长治市','晋城市','朔州市','晋中市','运城市','忻州市','临汾市','吕梁市')),
			array('mName'=>'四川省',
			'mItems' => array ('成都市','自贡市','攀枝花市','泸州市','德阳市','绵阳市','广元市','遂宁市','内江市','乐山市','南充市','眉山市','宜宾市','广安市','达州市','雅安市','巴中市','资阳市','阿坝藏族羌族自治州','甘孜藏族自治州','凉山彝族自治州')),
			array('mName'=>'贵州省',
			'mItems' => array ('贵阳市','六盘水市','遵义市','安顺市','铜仁地区','黔西南布依族苗族自治州','毕节地区','黔东南苗族侗族自治州','黔南布依族苗族自治州')),
			array('mName'=>'安徽省',
			'mItems' => array ('合肥市','芜湖市','蚌埠市','淮南市','马鞍山市','淮北市','铜陵市','安庆市','黄山市','滁州市','阜阳市','宿州市','巢湖市','六安市','亳州市','池州市','宣城市')),
			array('mName'=>'江西省',
			'mItems' => array ('南昌市','景德镇市','萍乡市','九江市','新余市','鹰潭市','赣州市','吉安市','宜春市','抚州市','上饶市')),
			array('mName'=>'云南省',
			'mItems' => array ('昆明市','曲靖市','玉溪市','保山市','昭通市','丽江市','思茅市','临沧市','楚雄彝族自治州','红河哈尼族彝族自治州','文山壮族苗族自治州','西双版纳傣族自治州','大理白族自治州','德宏傣族景颇族自治州','怒江傈僳族自治州','迪庆藏族自治州')),
			array('mName'=>'内蒙古自治区',
			'mItems' => array ('呼和浩特市','包头市','乌海市','赤峰市','通辽市','鄂尔多斯市','呼伦贝尔市','巴彦淖尔市','乌兰察布市','兴安盟','锡林郭勒盟','阿拉善盟')),
			array('mName'=>'广西壮族自治区',
			'mItems' => array ('南宁市','柳州市','桂林市','梧州市','北海市','防城港市','钦州市','贵港市','玉林市','百色市','贺州市','河池市','来宾市','崇左市')),
			array('mName'=>'西藏自治区',
			'mItems' => array ('拉萨市','昌都地区','山南地区','日喀则地区','那曲地区','阿里地区','林芝地区')),
			array('mName'=>'宁夏回族自治区',
			'mItems' => array ('银川市','石嘴山市','吴忠市','固原市','中卫市')),
			array('mName'=>'海南省',
			'mItems' => array ('海口市','三亚市')), 
			);
	}
	 
}
